1046. Last Stone Weight
题目 1046. Last Stone Weight
思路分析
优先队列实现
代码实现
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int stone:stones){
pq.offer(stone);
}
while(pq.size()>1){
int x = pq.poll();
int y = pq.poll();
if(x>y){
pq.offer(x-y);
}
}
return pq.isEmpty() ? 0 : pq.peek();
}
}
💬 评论